package vn.edu.tdt.it.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import vn.edu.tdt.it.common.OWLConst;
import vn.edu.tdt.it.common.VietnameseProcessing;

public class PropertyMapBuilder {
	public static Map<String, Object> buildProp(File file) throws IOException, SQLException{
		if (file.canRead()) {
			List<String> titleList = null;
			HashMap<String, Object> propMap = new HashMap<String, Object>();
			HashMap<String, String> retMap = new HashMap<String, String>();
			FileInputStream fs = new FileInputStream(file);
			BufferedReader bf = new BufferedReader(new InputStreamReader(fs));
			String line = null;
			while ((line = bf.readLine()) !=null) {
				String sql = "Select title from entity where id = ?";
				PreparedStatement ps = StaticConnection.getPreparedStatement(sql);
				ps.setString(1, line);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					retMap.put(line, VietnameseProcessing.removeSpecialChar(rs.getString(1)));
				}
			}
			titleList = new ArrayList<String>(retMap.keySet());
			propMap.put(OWLConst.MP_PROPERTY, retMap);
			propMap.put(OWLConst.LST_PROPERTY,titleList);
			return propMap;
		}
		return null;
	}
}
